package com.zl.tby.core.base.util;

import com.zl.tby.core.common.R;
import com.zl.tby.kafka.KafkaListener;
import com.zl.tby.kafka.UavList;
import com.zl.tby.kafka.UavListItem;
import com.zl.tby.kafka.UavNotify;
import com.zl.tby.model.TbyUavInfo;
import com.zl.tby.model.TbyUavInfoBackups;
import com.zl.tby.model.TbyUavKafka;
import com.zl.tby.service.*;
import com.zl.tby.vo.TbyUavRecordForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.noear.solon.Solon;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

import static com.zl.tby.core.base.util.Pg.saveUavTrack;

/**
 * @author changpeng
 * @since 2025/11/12 9:56
 */
public class Demo {

    private static Map<String, List<KafkaListener.FlightData>> deviceDataMap = new HashMap<>();

    public static void start() {
        ITbyUavKafkaService service = Solon.context().getBean(ITbyUavKafkaService.class);
        String[] content = new String[10];
        content[0] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>220</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:10</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037808683</Longitude>
                <Latitude>25.9649307671</Latitude>
                <Altitude>1119068</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>3</Pitch>
                <Roll>-26</Roll>
                <Yaw>1067</Yaw>
                <Heading>106</Heading>
                </Attitude>
                <Wind>
                <Speed>10.000000</Speed>
                <Direction>6</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[1] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>222</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:11</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037810162</Longitude>
                <Latitude>25.9649299060</Latitude>
                <Altitude>1118327</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>-3</Pitch>
                <Roll>0</Roll>
                <Yaw>1065</Yaw>
                <Heading>106</Heading>
                </Attitude>
                <Wind>
                <Speed>9.000000</Speed>
                <Direction>5</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[2] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>223</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:12</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037802114</Longitude>
                <Latitude>25.9649296550</Latitude>
                <Altitude>1118019</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>7</Pitch>
                <Roll>0</Roll>
                <Yaw>1064</Yaw>
                <Heading>106</Heading>
                </Attitude>
                <Wind>
                <Speed>9.000000</Speed>
                <Direction>1</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[3] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>224</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:13</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037800238</Longitude>
                <Latitude>25.9649292530</Latitude>
                <Altitude>1117665</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>2</Pitch>
                <Roll>10</Roll>
                <Yaw>1066</Yaw>
                <Heading>106</Heading>
                </Attitude>
                <Wind>
                <Speed>9.000000</Speed>
                <Direction>1</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[4] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>225</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:14</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037804559</Longitude>
                <Latitude>25.9649293873</Latitude>
                <Altitude>1117435</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>2</Pitch>
                <Roll>7</Roll>
                <Yaw>1070</Yaw>
                <Heading>107</Heading>
                </Attitude>
                <Wind>
                <Speed>10.000000</Speed>
                <Direction>2</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[5] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>226</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:15</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037799448</Longitude>
                <Latitude>25.9649281359</Latitude>
                <Altitude>1117317</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>-7</Pitch>
                <Roll>-7</Roll>
                <Yaw>1075</Yaw>
                <Heading>107</Heading>
                </Attitude>
                <Wind>
                <Speed>9.000000</Speed>
                <Direction>1</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[6] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>228</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:16</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037808832</Longitude>
                <Latitude>25.9649287555</Latitude>
                <Altitude>1116255</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>0</Pitch>
                <Roll>26</Roll>
                <Yaw>1078</Yaw>
                <Heading>107</Heading>
                </Attitude>
                <Wind>
                <Speed>10.000000</Speed>
                <Direction>1</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[7] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>229</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:17</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <UAVID></UAVID>
                <Model></Model>
                <Firmware></Firmware>
                <Manufactor></Manufactor>
                <CustomDevice></CustomDevice>
                <RecordLabel>1581F5FJD22BJ00B2GM2-20251105-vo90YhlF</RecordLabel>
                <Pos>
                <Longitude>117.5037829073</Longitude>
                <Latitude>25.9649290087</Latitude>
                <Altitude>1115242</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>-2</Pitch>
                <Roll>4</Roll>
                <Yaw>1079</Yaw>
                <Heading>107</Heading>
                </Attitude>
                <Wind>
                <Speed>12.000000</Speed>
                <Direction>1</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[8] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>230</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:18</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037857919</Longitude>
                <Latitude>25.9649310708</Latitude>
                <Altitude>1114783</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>9</Pitch>
                <Roll>-9</Roll>
                <Yaw>1080</Yaw>
                <Heading>108</Heading>
                </Attitude>
                <Wind>
                <Speed>11.000000</Speed>
                <Direction>4</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        content[9] = """
                <?xml version="1.0" encoding="gb2312"?>
                <Notify>
                <CmdType>DataTransfer</CmdType>
                <SN>231</SN>
                <DeviceID>10000000002000000004</DeviceID>
                <Time>2025-11-05T14:31:19</Time>
                <MetaVersion>v2</MetaVersion>
                <UAV>
                <UAVList Num="1">
                <Item>
                <Pos>
                <Longitude>117.5037854994</Longitude>
                <Latitude>25.9649302506</Latitude>
                <Altitude>1114585</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </Pos>
                <Speed>
                <X>0</X>
                <Y>0</Y>
                <Z>0</Z>
                </Speed>
                <Attitude>
                <Pitch>48</Pitch>
                <Roll>-4</Roll>
                <Yaw>1082</Yaw>
                <Heading>0</Heading>
                </Attitude>
                <Wind>
                <Speed>12.000000</Speed>
                <Direction>4</Direction>
                </Wind>
                <HomePoint>
                <Longitude>117.5037824655</Longitude>
                <Latitude>25.9649808902</Latitude>
                <Altitude>1125299</Altitude>
                <GPSStatus>0</GPSStatus>
                <GPSAltType>1</GPSAltType>
                <GPSCoorType>0</GPSCoorType>
                </HomePoint>
                </Item>
                </UAVList>
                </UAV>
                </Notify>
                """;
        for (int i = 0; i < content.length; i++) {
            try {
                Thread.sleep(2000);
                R<UavNotify> r = service.add(content[i]);
                if (r.getCode() == 0) {
                    UavNotify notify = r.getData();
                    receivingData(notify);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    // 获取数据
    private static void receivingData(UavNotify notify) {
        String deviceId = notify.getDeviceID(); // 获取设备ID
        // 假设 uavNotify 对象已经包含了相关的数据
        List<UavList> uavList = notify.getUav(); // 获取uav列表
        if (uavList != null && !uavList.isEmpty()) {
            UavList uav = uavList.get(0); // 获取列表中的第一个 UavList
            List<UavListItem> uavItems = uav.getItem(); // 获取该 UavList 中的 item 列表
            if (uavItems != null && !uavItems.isEmpty()) {
                UavListItem uavListItem = uavItems.get(0); // 获取 item 列表中的第一个 UavListItem
                String posLongitude = uavListItem.getPosLongitude(); // 获取经度
                String posLatitude = uavListItem.getPosLatitude(); // 获取纬度
                String attitudeHeading = uavListItem.getAttitudeHeading();
                if (attitudeHeading == null || attitudeHeading.isEmpty()) {
                    attitudeHeading = "0";
                }
                // 将真高从 String 转换为 int 类型
                int heading = Integer.parseInt(attitudeHeading);

                // 如果经度或纬度为空，则不处理这条数据，直接退出
                if (posLongitude == null || posLongitude.isEmpty() || posLatitude == null || posLatitude.isEmpty() || attitudeHeading == null || attitudeHeading.isEmpty()) {
                    return;
                }

                // 将经度和纬度转换为 double 类型
                double longitude = Double.parseDouble(posLongitude);
                double latitude = Double.parseDouble(posLatitude);


                // 进入处理map，看之前的map里是否存储，没存储就是起飞，有存储就是正在飞
                calculatedData(deviceId, longitude, latitude, heading);
            }
        }
    }

    public static void calculatedData(String deviceId, double lon,double lat,int currentHeading) {
        ITbyUavInfoBackupsService iTbyUavInfoBackupsService = Solon.context().getBean(ITbyUavInfoBackupsService.class);
        ITbyUavPointService tbyUavPointService = Solon.context().getBean(ITbyUavPointService.class);

        // 定义变量来存储经度和纬度
        double longitude = 0.0;
        double latitude = 0.0;
        double distance = 0.0;
        int time = 0;
        String latLogRecord = "";
        long UAVId = 0;
        String dealLatLogRecord = "";
        //先判断map里是否存在这个设备
        if (deviceDataMap.containsKey(deviceId)) {

            //如果进入这里面高度还是0，那么证明他没起飞，返回
            if (currentHeading == 0 && deviceDataMap.get(deviceId).get(0).getDistance() == 0) {
                return;
            }

            //获取一下之前map中的经纬度，计算出来飞行距离，时间增加
            // 获取该设备对应的FlightData列表
            List<KafkaListener.FlightData> dataList = deviceDataMap.get(deviceId);

            // 假设取列表中的第一个FlightData对象来获取经纬度（根据需求可以调整）
            if (!dataList.isEmpty()) {
                KafkaListener.FlightData flightData = dataList.get(0); // 这里可以根据需求选择获取的列表项
                longitude = flightData.getLongitude();
                latitude = flightData.getLatitude();
                //map中的总里程
                distance = flightData.getDistance();
                //map中的飞行时间
                time = flightData.getTime();
                //map中的经纬度字符串
                latLogRecord = flightData.getLatLogRecord();
                //map中的飞行机id
                UAVId = flightData.getUAVId();
            }

            //本次飞行的公里数
            double kilometers = haversine(longitude, latitude, lon, lat);
            System.out.printf("移动距离: %.6f 公里%n", kilometers);

            latLogRecord = latLogRecord + "," + lon + " "+ lat;
            System.out.println("每次拼接后：" + latLogRecord);
            dealLatLogRecord = latLogRecord;

            // 找到第一个逗号的位置
            int commaIndex1 = dealLatLogRecord.indexOf(',');

            // 如果找到逗号，则从逗号之后的部分开始截取
            if (commaIndex1 != -1) {
                // 截取从第一个逗号后面的部分
                dealLatLogRecord = dealLatLogRecord.substring(commaIndex1 + 1);
            }

            // 创建新的FlightData对象包含更新后的信息
            KafkaListener.FlightData updatedFlightData = new KafkaListener.FlightData(distance + kilometers, time + 1, lon, lat, currentHeading, latLogRecord, UAVId);

            // 更新列表中的第一个元素或根据需求更新其他元素
            dataList.set(0, updatedFlightData);

            // 更新Map中的数据
            deviceDataMap.put(deviceId, dataList);

            // 实时更新数据库中backup表
            TbyUavInfoBackups uavInfoBackups = new TbyUavInfoBackups();
            uavInfoBackups.setId(updatedFlightData.getUAVId()); //id
            uavInfoBackups.setDeviceId(deviceId); //设备id
            uavInfoBackups.setFlightDistance(BigDecimal.valueOf(updatedFlightData.getDistance()));//飞行里程
            uavInfoBackups.setFlightTime(updatedFlightData.getTime());//飞行时长
            uavInfoBackups.setLongitudeLatitudeStr(dealLatLogRecord);//经度纬度字符串
            uavInfoBackups.setCreateTime(LocalDateTime.now());//飞行时间
            uavInfoBackups.setDerail((short) 1);
            iTbyUavInfoBackupsService.addOrUpdate(uavInfoBackups);
            tbyUavPointService.add(deviceId, lon + "," + lat);

            if (currentHeading == 0) {//结束了

                ITbyUavInfoService iTbyUavInfoService = Solon.context().getBean(ITbyUavInfoService.class);
                ITbyUavRecordService tbyUavRecordService = Solon.context().getBean(ITbyUavRecordService.class);

                //TODO 此处操作数据库，并且清空对应map中的id数据
                double sumDistance = distance + kilometers;
                int sumTime = time + 1;

                //将类型转换为相同类型，方便之后运算
                BigDecimal sumTimeBigDecimal = new BigDecimal(sumTime);
                // 将 sumDistance 转换为 BigDecimal
                BigDecimal sumDistanceBigDecimal = new BigDecimal(sumDistance);

                System.out.println("开始进行插入数据库操作：" + deviceId);
                System.out.println("总里程：" + sumDistance);
                System.out.println("总飞行时间：" + sumTime);
                System.out.println("总经度纬度：" + latLogRecord);

                // 找到第一个逗号的位置
                int commaIndex = latLogRecord.indexOf(',');

                // 如果找到逗号，则从逗号之后的部分开始截取
                if (commaIndex != -1) {
                    // 截取从第一个逗号后面的部分
                    latLogRecord = latLogRecord.substring(commaIndex + 1);
                }

                //插入自己的备份表
                TbyUavInfoBackups tbyUavInfoBackups = new TbyUavInfoBackups();
                tbyUavInfoBackups.setId(UAVId); //设备id
                tbyUavInfoBackups.setDeviceId(deviceId); //设备id
                tbyUavInfoBackups.setFlightDistance(BigDecimal.valueOf(sumDistance));//飞行里程
                tbyUavInfoBackups.setFlightTime(sumTime);//飞行时长
                tbyUavInfoBackups.setLongitudeLatitudeStr(latLogRecord);//经度纬度字符串
                tbyUavInfoBackups.setCreateTime(LocalDateTime.now());//飞行时间
                tbyUavInfoBackups.setDerail((short) 2);
                iTbyUavInfoBackupsService.addOrUpdate(tbyUavInfoBackups);

                //插入无人机图层的空间数据
                saveUavTrack(deviceId,convertToDate(LocalDateTime.now()),latLogRecord);

                //根据设备id获取设备信息
                List<TbyUavInfo> infoList = iTbyUavInfoService.selectByDeviceId(deviceId);
                if (infoList == null || infoList.isEmpty()) {
                    return;
                } else {
                    Long infoId = infoList.get(0).getId();
                    BigDecimal infoDistance = infoList.get(0).getFlightDistance();//总里程
                    Long infoFlightCount = infoList.get(0).getFlightCount();//总次数
                    BigDecimal infoTime = infoList.get(0).getFlightTime();//总时长

                    //求和
                    BigDecimal finalDistance = infoDistance.add(sumDistanceBigDecimal);//最终总里程
                    Long finalFlightCount = (infoFlightCount != null ? infoFlightCount : 0L) + 1; // 最终总次数
                    BigDecimal finalTime = infoTime.add(sumTimeBigDecimal);//最终总时长

                    //修改无人机信息
                    iTbyUavInfoService.updateByDeviceId(infoId, finalDistance, finalTime, finalFlightCount);

                    // 记录飞行任务
                    TbyUavRecordForm record = new TbyUavRecordForm();
                    record.setUavId(infoId);
                    record.setFlightTime(sumTimeBigDecimal);
                    record.setFlightDistance(sumDistanceBigDecimal);
                    record.setUseDate(new Date(System.currentTimeMillis()));
                    record.setTaskCode("0");
                    tbyUavRecordService.add(record);

                    removeDataByDeviceId(deviceId);
                }
            }
        } else {
            // 设备不存在，创建一个新的FlightData对象，并初始化飞行时间和飞行距离为0

            // 使用 String.format() 保留六位小数
            latLogRecord = String.format("%.6f %.6f", lon, lat);
            System.out.println("初始化：" + latLogRecord);

            //定义一个无人机id
            UAVId = generateId();

            KafkaListener.FlightData newData = new KafkaListener.FlightData(0.0, 0, lon, lat, currentHeading, latLogRecord, UAVId); // 假设FlightData构造函数接收距离和时间

            // 创建一个列表来存储FlightData
            List<KafkaListener.FlightData> dataList = new ArrayList<>();
            dataList.add(newData);

            // 将设备ID和数据列表存入Map中
            deviceDataMap.put(deviceId, dataList);

            // 输出确认信息
            System.out.println("已为设备 " + deviceId + " 添加新数据: " + newData);
        }
    }

    //计算飞行距离
    private static double haversine(double lon1, double lat1, double lon2, double lat2) {
        final int R = 6371; // 地球半径，单位为公里

        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.asin(Math.sqrt(a));

        return R * c; // 返回公里数
    }

    // 重置操作
    private static void removeDataByDeviceId(String deviceId) {
        // 检查设备 ID 是否存在
        if (deviceDataMap.containsKey(deviceId)) {
            // 删除该设备 ID 对应的数据
            deviceDataMap.remove(deviceId);
            System.out.println("删除设备信息: " + deviceId);
        } else {
            // 如果设备 ID 不存在，不做任何操作
            System.out.println("未找到设备信息: " + deviceId);
        }
    }

    // 转换方法
    public static Date convertToDate(LocalDateTime localDateTime) {
        // 使用系统默认时区将 LocalDateTime 转换为 Date
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 根据时间戳生成id
     * @author AnWeiNan
     * @date 2024/11/11 下午3:51
     */
    public static long generateId() {
        // 获取当前时间戳的毫秒数
        long timestamp = System.currentTimeMillis();

        // 将当前时间戳转换为字符串
        String idStr = String.valueOf(timestamp);

        // 如果 idStr 长度大于 11，则截取最后 11 位
        if (idStr.length() > 11) {
            idStr = idStr.substring(idStr.length() - 11);
        }

        // 如果 idStr 长度小于 11，在前面补0，确保长度为11
        while (idStr.length() < 11) {
            idStr = "0" + idStr;
        }

        // 将字符串转换为 long 类型
        long id = Long.parseLong(idStr);

        return id;
    }
}
